package leetcode;

import java.util.HashSet;
import java.util.Set;

public class LeetCodeMain116 {

    public int findNthDigit(int n) {
        int digit=1;
        long start=1;
        long count=9;
        while (n>count){
            n-=count;
            digit++;
            start=start*10;
            count=digit*start*9;
        }
        long num=start+(n-1)/digit;
        return Long.toString(num).charAt((n - 1) % digit) - '0';
    }
    public int lengthOfLongestSubstring(String s) {
        if (s==null||s.length()==0){
            return 0;
        }
        int max=0;
        for (int i = 0; i < s.length(); i++) {
            Set<Character> set=new HashSet<>();
            set.add(s.charAt(i));
            int count=1;
            for (int j = i+1; j < s.length(); j++) {
                char ch=s.charAt(j);
                if (set.add(ch)){
                    count++;
                }else {
                    break;
                }
            }
            max=Math.max(max,count);
        }
        return max;
    }
    public int strToInt(String str) {
        str=str.trim();
        long sum=0;
        if (str==null||str.length()==0){
            return 0;
        }
        if (!Character.isDigit(str.charAt(0))&&str.charAt(0)!='+'&&str.charAt(0)!='-'){
            return 0;
        }
        boolean flag = str.charAt(0) == '-';
        int i=Character.isDigit(str.charAt(0))?0:1;
        for (; i < str.length()&&Character.isDigit(str.charAt(i)); i++) {
            char ch=str.charAt(i);
            sum=sum*10+ch-'0';
            if (!flag&&sum>=Integer.MAX_VALUE){
                sum=Integer.MAX_VALUE;
                break;
            }
            if (flag&&sum*-1<=Integer.MIN_VALUE){
                sum=Integer.MIN_VALUE;
                break;
            }
        }
        return  flag?(int)-sum:(int)sum;
    }
}
